﻿using System;
using System.IO;
using System.Reflection;

using SBPweb.MediaCenter.Common.Resources;

namespace SBPweb.MediaCenter.Common.Modularity
{
    internal struct ModuleDescriptor
    {

        private byte[] mRawData;
        private string mModuleTypeName;

        internal ModuleDescriptor(FileInfo fileInfo)
        {
            try
            {
                mRawData = null;
                mModuleTypeName = null;

                mRawData = GetBytes(fileInfo.FullName);
                Assembly asm = Assembly.LoadFrom(fileInfo.FullName);

                object obj = ResourceHandler.GetAssemblyCustomAttribute<AssemblyDefaultAliasAttribute>(asm);

                mModuleTypeName = obj.ToString();
            }
            catch (Exception ex)
            {
                throw new ArgumentException("FileInfo parameter error!", ex);
            }
        }

        internal byte[] RawData
        {
            get
            {
                return mRawData;
            }
        }

        internal string ModuleTypeName
        {
            get
            {
                return mModuleTypeName;
            }
        }

        internal static byte[] GetBytes(string path)
        {
            byte[] rawData;
            FileStream fS = new FileStream(path, FileMode.Open, FileAccess.Read);
            int length = (int)fS.Length;
            rawData = (byte[])Array.CreateInstance(typeof(byte), length);
            fS.Read(rawData, 0, length);
            fS.Close();

            return rawData;
        }


    }
}